package initializer

import "sgo/scontext"

type Initializer interface {
	initialize(sc *scontext.SgoContext) error
}

type InitializeWrapper struct {
	err error
}

func (iw *InitializeWrapper) wrapperFunc(ctx *scontext.SgoContext, initializer Initializer) {
	if iw.err != nil {
		return
	}
	// 初始化配置文件需要放在首位
	iw.err = initializer.initialize(ctx)
}

func Initialize(sc *scontext.SgoContext) error {

	initializeWrapper := InitializeWrapper{}

	initializeWrapper.wrapperFunc(sc, ViperInitializer{})
	initializeWrapper.wrapperFunc(sc, ZapInitializer{})
	initializeWrapper.wrapperFunc(sc, ValidatorInitializer{})
	initializeWrapper.wrapperFunc(sc, RedisInitializer{})
	initializeWrapper.wrapperFunc(sc, GormInitializer{})
	initializeWrapper.wrapperFunc(sc, SwaggerInitializer{})

	if initializeWrapper.err != nil {
		return initializeWrapper.err
	}
	return nil
}
